procedure PASCALMAIN; external name 'PASCALMAIN';

{******************************************************************************
                        Shared library start/halt
 ******************************************************************************}
{$asmmode ATT}

procedure KaffeOS_start; public name 'FPC_LIB_START';
begin
  IsLibrary:=true;

  PASCALMAIN;
end;

procedure KaffeOS_stop; public name 'FPC_LIB_EXIT';
begin

end;

procedure _FPC_shared_lib_start(argc : dword;argv,envp : pointer); cdecl; public name '_FPC_SHARED_LIB_START_LOCAL'; public name '_start';
begin
  { we've to discuss about the use of this ;) }
  {asm
    movl    %esp,initialstkptr
  end;}

  IsLibrary:=true;

  PASCALMAIN;
end;

{$ifndef VER2_0}

{ this hack is needed so we can make the reference below to _FPC_shared_lib_start }
{ local in compiler/systems/t_linux.pas                                           }
procedure _FPC_SHARED_LIB_START_LOCAL(argc : dword;argv,envp : pointer); cdecl; external;

procedure initdummy; assembler; nostackframe;
label
  FPC_LIB_START;
asm
.init
  .align 16
  .globl FPC_LIB_START
//    .type FPC_LIB_START,@function
FPC_LIB_START:
{$ifdef FPC_PIC}
  jmp	_FPC_SHARED_LIB_START_LOCAL@PLT
{$else FPC_PIC}
  jmp	_FPC_SHARED_LIB_START_LOCAL
{$endif FPC_PIC}
.text
end;
{$endif VER_2_0}

procedure _FPC_shared_lib_haltproc; assembler; nostackframe; public name '_haltproc';
asm
{$ifdef FPC_PIC}
  call    fpc_geteipasebx
  addl    $_GLOBAL_OFFSET_TABLE_,%ebx
{$endif}
.Lhaltproc:
  xorl    %eax,%eax
  incl    %eax                    { eax=1, exit call }
{$ifdef FPC_PIC}
  pushl   %ebx
  movl    ExitCode@GOT(%ebx),%ebx
{$if sizeof(ExitCode)=2}
  movzwl  (%ebx),%ebx
{$else}
  mov     (%ebx),%ebx
{$endif}
{$endif}
  int     $0x80
  jmp     .Lhaltproc
  popl    %ebx
end;